package com.tl.khadmin.service.impl;

import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.tl.khadmin.bean.CostAdjust;
import com.tl.khadmin.bean.Order;
import com.tl.khadmin.bean.Staff;
import com.tl.khadmin.bean.Staffrole;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.bean.SupplierBankAccount;
import com.tl.khadmin.mapper.CostAdjustMapper;
import com.tl.khadmin.mapper.OrderMapper;
import com.tl.khadmin.mapper.StaffMapper;
import com.tl.khadmin.mapper.StaffroleMapper;
import com.tl.khadmin.mapper.SupplierBankAccountMapper;
import com.tl.khadmin.mapper.SupplierMapper;
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.CostAdjustIService;
import com.tl.khadmin.service.StaffIService;
import com.tl.khadmin.util.CommonUtil;
import com.tl.khadmin.util.Constant;
import com.tl.khadmin.util.PutParamUtil;

@Service
public class CostAdjustServiceImpl implements CostAdjustIService{
	
	@Resource
	private CostAdjustMapper costAdjustMapper; 
	@Resource
	private OrderMapper orderMapper;
	
	@Override
	public Page<CostAdjust> showByPage(HttpSession session, int pageNum, int pageSize, Map<String, Object> params) {
		Page<CostAdjust> page=new Page<CostAdjust>(pageNum, pageSize);
        params.put("page", page);   
        Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
        params.put("supplierId", supplier.getId());
        PutParamUtil.setParamIsInterior(supplier, params);	//设置查询供应商数据范围 
        List<CostAdjust> list = costAdjustMapper.showByPage(params);  
		page.setResult(list);
		return page;
	}
	
	@Override
	public String save(HttpSession session, CostAdjust costAdjust) {  
		//如存在订单号
		if(!StringUtils.isEmpty(costAdjust.getOrderSn())) { 
			Order order = orderMapper.findByOrderId(costAdjust.getOrderSn());
			if(order == null) {
				return CommonUtil.returnMapResultToJson("fail", "订单不存在，请重新输入");
			}
			costAdjust.setOrderId(order.getId());
		}
		
		Staff staff = (Staff) session.getAttribute(Constant.USER);
		costAdjust.setCreateTime(new Date());
		costAdjust.setCreateId(staff.getId());  
		int result = costAdjustMapper.insert(costAdjust);
		return CommonUtil.returnMapResultByRows(result, "提交成功", "提交失败，请重试");
	}
	
	
//	@Override
//	public String deleteStaff(Integer id) {
//		try {
//			return CommonUtil.returnMapResultByRows(staffMapper.deleteByPrimaryKey(id), "删除成功", "删除失败");
//		} catch (Exception e) { 
//			return CommonUtil.returnMapResultByRows(0, "删除成功", "删除异常");
//		}  
//	}
//	

//	
//	@Override
//	public Staff queryById(Integer id) {
//		return staffMapper.selectByPrimaryKey(id);
//	}
//	
//	@Override
//	public String updateStaff(Staff staff) {
//
//		Staff lastStaff = staffMapper.selectByPrimaryKey(staff.getId());
//		lastStaff.setNickname(staff.getNickname());
//		lastStaff.setRole(staff.getRole());
//		lastStaff.setPhone(staff.getPhone());
//		int result = staffMapper.updateByPrimaryKeySelective(lastStaff);
//		return CommonUtil.returnMapResultByRows(result, "修改成功","修改失败，请重试");
//	}
	
}